﻿using System;
using System.ComponentModel;
using AiFrame.Base.Core.Data.Access;

namespace AiFrame.Base.Core.MVP
{
    /// <summary>
    /// Presenter for the main window view.
    /// </summary>
    public class MainWindowPresenter
    {
        /// <summary>
        /// The view.
        /// </summary>
        private IMainWindowView _view;
        
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="view">The view.</param>
        public MainWindowPresenter(IMainWindowView view)
        {
            this._view = view;

            /* Den TabSetManager initialisieren */
            TabSetManager tabSetManager = TabSetManager.Instance();
            tabSetManager.Initialize(_view);
        }

        /// <summary>
        /// Initializes the presenter.
        /// </summary>
        public void Initialize()
        {
            DatabaseConnection.Instance.PropertyChanged += new PropertyChangedEventHandler(Connection_Property_Changed);
            RefreshView();
        }

        /// <summary>
        /// Is called when the database connection status
        /// has changed.
        /// </summary>
        /// <param name="sender">The sender class.</param>
        /// <param name="e">The even arguments.</param>
        private void Connection_Property_Changed(object sender, PropertyChangedEventArgs e)
        {
            RefreshView();
        }

        /// <summary>
        /// Refreshes the view.
        /// </summary>
        private void RefreshView()
        {
            if (DatabaseConnection.Instance.Connected)
                _view.SetOnlineStatusText(String.Format("Online - {0}", DatabaseConnection.Instance.Username));
            else
                _view.SetOnlineStatusText("Offline");
        }

        /// <summary>
        /// Is called when the user wants to close the main view.
        /// </summary>
        public void UserWantsToClose()
        {
            _view.Close();
        }
    }
}